package com.lnking.lnkingplace.service.impl;


import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.Util.StringUtil;
import com.lnking.lnkingplace.mapper.MeetingMapper;
import com.lnking.lnkingplace.model.dto.isolation.assistance.AssistanceImportDto;
import com.lnking.lnkingplace.model.dto.meeting.MeetingDetailDto;
import com.lnking.lnkingplace.model.dto.meeting.MeetingImportDto;
import com.lnking.lnkingplace.model.dto.user.UserDetailDto;
import com.lnking.lnkingplace.model.entity.IsolationAssistance;
import com.lnking.lnkingplace.model.entity.Meeting;
import com.lnking.lnkingplace.model.entity.RecordUser;
import com.lnking.lnkingplace.model.entity.SpecialPlace;
import com.lnking.lnkingplace.model.request.meeting.MeetingAddRequest;
import com.lnking.lnkingplace.model.request.meeting.MeetingUpdateRequest;
import com.lnking.lnkingplace.service.IMeetingService;
import com.lnking.lnkingplace.service.ISpecialPlaceService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-09-18
 */
@Service
public class MeetingServiceImpl extends CustomServiceImpl<MeetingMapper, Meeting> implements IMeetingService {

    @Autowired
    ISpecialPlaceService specialPlaceService;

    @Override
    public void add(MeetingAddRequest params) {
        Meeting entity = new Meeting();
        BeanUtils.copyProperties(params, entity);
        getBaseMapper().insert(entity);
    }

    @Override
    public void update(MeetingUpdateRequest params) {
        Meeting entity = new Meeting();
        BeanUtils.copyProperties(params, entity);
        getBaseMapper().updateById(entity);
    }

    @Override
    public MeetingDetailDto getDetailById(String id) {
        return this.getBaseMapper().DetailById(id);
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<MeetingImportDto> result =  ImportUtils.importExcel(file, MeetingImportDto.class,option);
        List<Meeting> list=new ArrayList<>();
        List<MeetingImportDto> collect = result.stream().filter(e ->e.getName() != null&& !"".equals(e.getName())).collect(Collectors.toList());
       // 处理场所
        collect.forEach(e->{
            Meeting meeting = new Meeting();
            BeanUtils.copyProperties(e,meeting);
            // 处理场所
            SpecialPlace byName = specialPlaceService.getByName(e.getPlaceName());
            meeting.setPlaceId(byName.getId());
            // 处理时间
            if (e.getStartTime()!=null&& !"".equals(e.getStartTime())) {
                meeting.setStartTime(HSSFDateUtil.getJavaDate(Double.parseDouble(e.getStartTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
              if(e.getEndTime() != null&& !"".equals(e.getEndTime())){
                  meeting.setEndTime(HSSFDateUtil.getJavaDate(Double.parseDouble(e.getEndTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
              }else{
                  meeting.setEndTime(HSSFDateUtil.getJavaDate(Double.parseDouble(e.getStartTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
              }
            }
            // 处理类型
            if (e.getArea() != null&&!"".equals(e.getArea())) {
                System.out.println(e.getArea());
                meeting.setArea(Float.valueOf(e.getArea()));
            }
            if (e.getAccommodation() != null&&!"".equals(e.getAccommodation())) {
                System.out.println(e.getArea());
                meeting.setAccommodation(Float.valueOf(e.getAccommodation()));
            }
            if (e.getIncome() != null&&!"".equals(e.getIncome())) {
                System.out.println(e.getArea());
                meeting.setIncome(Float.valueOf(e.getIncome()));
            }
            if (e.getRental() != null&&!"".equals(e.getRental())) {
                System.out.println(e.getArea());
                meeting.setRental(Float.valueOf(e.getRental()));
            }
            if (e.getRestaurant() != null&&!"".equals(e.getRestaurant())) {
                System.out.println(e.getArea());
                meeting.setRestaurant(Float.valueOf(e.getRestaurant()));
            }

            list.add(meeting);
        });
        //批量插入
        this.saveBatch(list);
    }
}
